﻿using System;
using System.Collections.Generic;
using System.Net;
namespace PssFramework
{

    namespace PSServer
    {
        class Filter
        {
            static Dictionary<int, List<EndPoint>> _subscribersList = new Dictionary<int, List<EndPoint>>();

            static public Dictionary<int, List<EndPoint>> SubscribersList
            {
                get
                {
                    lock (typeof(Filter))
                    {
                        return _subscribersList;
                    }
                }

            }

            static public List<EndPoint> GetSubscribers(int Topic)
            {
                lock (typeof(Filter))
                {
                    if (SubscribersList.ContainsKey(Topic))
                    {
                        return SubscribersList[Topic];
                    }
                    else
                        return null;
                }
            }

            static public void AddSubscriber(int Topic, EndPoint subscriberEndPoint)
            {
                lock (typeof(Filter))
                {
                    if (SubscribersList.ContainsKey(Topic))
                    {
                        if (!SubscribersList[Topic].Contains(subscriberEndPoint))
                        {
                            SubscribersList[Topic].Add(subscriberEndPoint);
                        }
                    }
                    else
                    {
                        List<EndPoint> newSubscribersList = new List<EndPoint>();
                        newSubscribersList.Add(subscriberEndPoint);
                        SubscribersList.Add(Topic, newSubscribersList);
                    }
                }

            }

            static public void RemoveSubscriber(int Topic, EndPoint subscriberEndPoint)
            {
                lock (typeof(Filter))
                {
                    if (SubscribersList.ContainsKey(Topic))
                    {
                        if (SubscribersList[Topic].Contains(subscriberEndPoint))
                        {
                            SubscribersList[Topic].Remove(subscriberEndPoint);
                        }
                    }
                }
            }

        }
    }
}